package com.sun.huazhi.aitaskservice.common.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class RabbitMQConfig {
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//
//    @PostConstruct
//    public void init() {
//        // 消息到达Exchange的回调
//        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
//            if (ack) {
//                log.info("消息到达交换机: {}", correlationData);
//            } else {
//                log.error("消息发送到交换机失败: {}", cause);
//            }
//        });
//
//        // 消息无法路由到Queue的回调
//        rabbitTemplate.setReturnsCallback(returned -> {
//            log.error("消息无法路由到队列: {}", returned.getMessage());
//            // 可在此处记录失败消息或重试
//        });
//    }

    @Bean
    public Jackson2JsonMessageConverter jsonMessageConverter() {
        DefaultClassMapper classMapper = new DefaultClassMapper();
        classMapper.setTrustedPackages("java.util");
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
        converter.setClassMapper(classMapper);
        return converter;
    }
}
